package cn.q3c.download.commons;

import java.io.File;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;


/**
 * 下载日志
 * @author wangxinning
 *
 */
public class DownloadLog {

	private Date date;
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	private String url;
	private String name;
	private File file;
	private File parentFile;
	public File getParentFile() {
		return parentFile;
	}
	public void setParentFile(File parentFile) {
		this.parentFile = parentFile;
	}
	public String getParentUrl() {
		return parentUrl;
	}
	public void setParentUrl(String parentUrl) {
		this.parentUrl = parentUrl;
	}
	private String parentUrl;
	
	public String toString()
	{
		//，转换，避免解析错误
		return DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss") + ","
				+ StringUtils.trimToEmpty(url) + ","
				+ StringUtils.replace(StringUtils.trimToEmpty(name),",","，") + ","
				+ file.toString() + ","
				+ StringUtils.trimToEmpty(parentUrl) + ","
				+ ((parentFile != null)?parentFile.toString():"");
	}
	
	public DownloadLog()
	{
		
	}
	
	public DownloadLog( String log ) throws ParseException
	{
		//新格式
		String[] args = log.split(",");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.date = sdf.parse(args[0]);
		this.url=args[1];
		this.name=args[2];
		this.file=new File( args[3] );
		
		if( args.length > 5 )
		{
			this.parentUrl=args[4];
			this.parentFile=new File( args[5] );
		}
	}
}